package common.fengkai.filepost.utils;

import com.alibaba.fastjson.JSONObject;

import com.paic.sealcenter.webservice.ResponVo;
import common.fengkai.filepost.entity.Position;
import common.fengkai.filepost.entity.Seal;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
 * 测试电子签章接口
 */
public class SignTestTwo {

    private ResponVo sign_SM() throws Exception {
        //String url = "http://10.0.100.57:7001/iSignatureWebPDF/services/PdfActionService";
        //String url = "http://10.29.31.124:8002/eseal/iSignatureWebPDF/services/PdfIdActionService";
        //网关 http://10.29.31.61:8882/eseal/eseal/app/services/signPdf
        String url = "http://10.29.31.58:8002/eseal/iSignatureWebPDF/services/PdfIdActionService";
        //String url = "http://10.29.31.121:8882/eseal/eseal/iSignatureWebPDF/services/PdfIdActionService";
        long startTime = System.currentTimeMillis();
        //curl -H "Content-Type:application/json" -X POST -d '@body.txt' "http://10.30.48.249eseal/eseal/app/services/signPdf"
        //生产（http://10.30.48.249） uat（http://10.29.28.3）
        //String url = "http://10.29.28.3/eseal/eseal/iSignatureWebPDF/services/PdfIdActionService";
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.setServiceClass(com.paic.sealcenter.webservice.impl.PdfIdActionService.class);
        factoryBean.setAddress(url);
        com.paic.sealcenter.webservice.impl.PdfIdActionService pdfActionService = (com.paic.sealcenter.webservice.impl.PdfIdActionService) factoryBean.create();

        Client proxy = ClientProxy.getClient(pdfActionService);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        conduit.getClient().setReceiveTimeout(0L);
        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(60000L);
        policy.setAllowChunking(false);
        policy.setReceiveTimeout(1200000000L);
        conduit.setClient(policy);

        //要签章的文件
       // byte[] file = Files.readAllBytes(Paths.get("C:/Users/carry/Desktop/项目资料/非车简易测试文件/66801060620200000006.pdf"));
        byte[] file = Files.readAllBytes(Paths.get("C:/Users/carry/Desktop/电子保单测试/2.pdf"));
        //
        Signature signature = new Signature();
        List<SignSeal> signSealList = new ArrayList<>();
        //公章  圆形  太平财产保险有限公司-新洲支公司   太平财产保险有限公司陕西分公司
        //业务章 签名章 方形 太平财产保险有限公司青岛分公司承保电子专用章  太平财产保险有限公司深圳分公司中英文许少武授权签名章
        Seal seal1 = new Seal("太平财产保险有限公司亳州中心支公司承保专用章","dzbd");
        Seal seal2 = new Seal("太平财产保险有限公司亳州中心支公司承保专用章","dzbd");
        //Position position1 = new Position(0,450,300,null);
        //Position position4 = new Position(1,450,300,null);
        //Position position5 = new Position(2,450,300,null);
        Position position1 = new Position(null,null,null,"（保险人签章）");
        //SignSeal signSeal4 = new SignSeal("swxa",seal2,position4,1);
        //SignSeal signSeal5 = new SignSeal("swxa",seal2,position5,1);





        Position position2 = new Position(null,null,null,"（保险人签章）");
        SignSeal signSeal1 = new SignSeal("swxa",seal1,position1,1);
        SignSeal signSeal2 = new SignSeal("swxa",seal2,position2,1);
        signSealList.add(signSeal1);
        //signSealList.add(signSeal4);
        //signSealList.add(signSeal5);
        //signSealList.add(signSeal2);
        List<SignSeal> signSealList152 = get152SignSealList();

        signature.setSignature(signSealList);
        String jsonString = JSONObject.toJSONString(signature);
        System.out.println("jsonString="+jsonString);


        if (!Objects.equals("com.ctc.wstx.stax.WstxInputFactory", System.getProperty("javax.xml.stream.XMLInputFactory"))) {
            System.setProperty("javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory");
            System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory");
            System.setProperty("javax.xml.stream.XMLEventFactory", "com.ctc.wstx.stax.evt.WstxEventFactory");
        }

        /**
         * 获取返回结果  file  文件字节数组   msg  提示信息  code  状态码
         */
        com.paic.sealcenter.webservice.ResponVo result = pdfActionService.signedPDF(file, jsonString);
        String string = result.toString();
        if (result.getFile()!=null) {
            System.out.println("返回值msg="+result.getMsg());
            System.out.println("result.length:"+result.getFile().length);
            //太平财产保险有限公司青岛分公司中英文刘洪涛授权签名章
            //太平财产保险有限公司青岛分公司中英文刘洪涛授权签名章
            Files.write(Paths.get("C:/Users/carry/Desktop/电子保单测试/2signed.pdf"), result.getFile());
            long entTime = System.currentTimeMillis();
            System.out.println("花费了"+(entTime-startTime)/1000+"秒");
        }
        //Files.write(Paths.get("D:/TestSignd签名.pdf"), result);
        return result;
    }


    private  List<SignSeal> get152SignSealList (){
        List<SignSeal> signSealList = new ArrayList<>();
        Seal seal1 = new Seal("太平财产保险有限公司陕西分公司","sichuan");
        int count = 0;
        for (int i = 0; i <100 ; i++) {
            Position position = new Position(i,450,300,null);
            SignSeal signSeal1 = new SignSeal("swxa",seal1,position,1);
            signSealList.add(signSeal1);
            count+=1;

        }
        System.err.println("加盖了"+count+"个章");
        return signSealList;
    }



    public static void main(String[] args) throws Exception {
        SignTestTwo signTest = new SignTestTwo();
        com.paic.sealcenter.webservice.ResponVo r = signTest.sign_SM();
        JSONObject o = (JSONObject) JSONObject.toJSON(r);
        System.out.println(r.getMsg());

    }


}